#include "pagemanager.h"
#include <QApplication>
#include <QDebug>

PageManager::PageManager(MainWindow *mainWindow, QObject *parent)
    : QObject(parent), mainWindow(mainWindow) // 使用传入的mainWindow实例
{
  // 创建堆叠窗口
  stackedWidget = new QStackedWidget();
  stackedWidget->resize(800, 480);
  stackedWidget->setWindowFlags(Qt::FramelessWindowHint);

  // 创建广告页面
  adPage = new ad();

  // 添加到堆叠窗口
  stackedWidget->addWidget(adPage);
  stackedWidget->addWidget(mainWindow); // 使用传入的mainWindow实例

  // 创建返回广告页的定时器
  returnTimer = new QTimer(this);
  returnTimer->setSingleShot(true);
  connect(returnTimer, &QTimer::timeout, this, &PageManager::showAdPage);
}

PageManager::~PageManager() { delete stackedWidget; }

void PageManager::init() {
  // 初始显示广告页面
  showAdPage();
  stackedWidget->showFullScreen();
  qDebug() << "system start ad_page start";
}

void PageManager::showMainPage() {
  if (stackedWidget->currentWidget() == mainWindow) // 如果为主界面就不切换
    return;
  stackedWidget->setCurrentIndex(1); // 显示主页面
  qDebug() << "change home_page";
}

void PageManager::showAdPage() {
  stackedWidget->setCurrentIndex(0); // 显示广告页面
  returnTimer->stop();
  mainWindow->clear_label_text(); // 清空标签
  qDebug() << "back ad_page";
}

void PageManager::onRfidCardDetected(QString cardNumber) {
  qDebug() << "jian ce RFID card，change hoome_age card:" << cardNumber;
  card_number = cardNumber;

  showMainPage();
}

QTimer *PageManager::getQTimer() { return returnTimer; }

void PageManager::open_ReturnTimer() {
  returnTimer->start(returnDelay);
  qDebug() << "----=-=-=-=-=--=--=";
}
